/**
 * @description clear module
 * @author Zhangbin
 */

import { Commit } from 'vuex'
import store from '../index'
import { message } from 'ant-design-vue'
import { logoutApi } from '@/api/login'
import * as types from '@/store/mutation-types'
import { clearCookies } from '@/utils/cookies'

const state: object = {}

const mutations = {
  [types.CLEAR_ALLDATA](state: any, payload: any) {
    clearCookies();
    // clear userinfo
    store.commit('CLEAR_USERINFO');
    // clear router
    store.commit('CLEAR_ROUTER');
    // clear setting
    store.commit('CLEAR_SETTING');
  },
}

const actions = {
  [types.CLEAR_ALLDATA]: async (context: { commit: Commit, state: any }, payload: any): Promise<void> => {
    let res = await logoutApi({});
    res.data.code === 0
      ? (context.commit(types.CLEAR_ALLDATA, payload), message.success('退出成功！'))
      : message.error('退出失败！')
  }
}

export const clearStore = {
  state,
  mutations,
  actions
}